<template>
  <div class="container">
    <el-row>
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item
          label="文章标题："
          prop="title"
          style="width: 500px"
          :rules="[{ required: true, message: '文章标题不能为空' }]"
        >
          <el-input
            v-model="form.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item
          ref="img"
          label="封面图："
          prop="coverImage"
          :rules="[{ required: true, message: '封面图不能为空' }]"
        >
          <Upload
            @fileListChange="fileListChange"
            ref="myUpload"
            :drag="true"
            :accept="imgType"
            fileType="image"
            :filelist="myFileList"
          />
        </el-form-item>
        <el-form-item
          label="是否热门："
          prop="isHot"
          style="width: 300px; display: inline-block"
        >
          <el-switch v-model="form.isHot" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="内容：" prop="description">
          <tinymce
            v-model="form.content"
            :height="400"
            @input="handleContent"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')" :loading="loading"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import normalFilter from "@/components/NormalFilter";
import Upload from "@/components/UploadFile";
import Tinymce from "@/components/Tinymce";
import { Message } from "element-ui";
import { add, getDetail, update } from "@/api/article";
import { getList } from "@/api/productComb";
import { imgTypeArr } from "@/utils/enumConfig";

export default {
  name: "bannerAdd",
  components: {
    normalFilter,
    Tinymce,
    Upload
  },
  data() {
    return {
      loading: false,
      id: "",
      tags: [],
      inputVisible: false,
      inputValue: "",
      productCombList: [],
      imgType: imgTypeArr.join(","),
      myFileList: [],
      form: {
        title: "",
        content: "",
        coverImage: "",
        isHot:0
      }
    };
  },
  async mounted() {
    let res = await getList({ current: 1, size: 1000 });
    if (res) {
      this.productCombList = res.records;
    }
    let { id } = this.$route.params;
    if (!id) return;
    let info = await getDetail({ id });
    if (info) {
      if (info.coverImage) {
        let fileName = this.getFileName(info.coverImage);
        this.myFileList = [{ name: fileName, url: info.coverImage }];
      }
      this.form = info;
      this.id = id;
    }
  },
  methods: {
    getFileName(fileUrl) {
      let fileName = fileUrl.substring(
        fileUrl.lastIndexOf("/") + 1,
        fileUrl.length
      );
      return fileName;
    },
    onSubmit(formName) {
      const { validate } = this.$refs[formName];
      validate(valid => {
        if (valid) {
          this._submitData(this.form);
        }
      });
    },
    async _submitData(params) {
      this.loading = true;
      let res = !this.id
        ? await add(params)
        : await update({ ...params, id: this.id });
      this.loading = false;
      if (res.code === 1) {
        Message({
          message: "保存成功！",
          type: "success",
          duration: 1 * 1000
        });
        setTimeout(() => {
          this.$router.go(-1);
        }, 1000);
      }
    },
    handleContent(val) {
      this["form"]["content"] = val;
    },
    fileListChange(arr) {
      if (arr.length === 0) {
        this["form"]["coverImage"] = "";
      } else {
        this["form"]["coverImage"] = arr[0]["url"];
        this.$refs.img.clearValidate();
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  padding: 20px;
  .radio-form {
    ::v-deep .el-form-item__error {
      left: 5%;
    }
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>
